package com.yxhl.zoume.core.tripsmgmt.ui.fragment.tripsdetail;


import android.Manifest;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.yxhl.protobuf.BizOrder;
import com.yxhl.protobuf.BizOrderResponse;
import com.yxhl.protobuf.Contract;
import com.yxhl.protobuf.JudgeEntry;
import com.yxhl.protobuf.OrderBizType;
import com.yxhl.protobuf.OrderPayStatus;
import com.yxhl.zoume.R;
import com.yxhl.zoume.UIConstants;
import com.yxhl.zoume.common.ui.custom.CheckBoxLayout;
import com.yxhl.zoume.common.ui.custom.ListViewWrap;
import com.yxhl.zoume.common.ui.fragment.BaseFragment;
import com.yxhl.zoume.core.func.appraisal.model.CommentOnDriverEvent;
import com.yxhl.zoume.core.tripsmgmt.info.CommentEntrance;
import com.yxhl.zoume.core.tripsmgmt.info.ZouMeBusMapEntrance;
import com.yxhl.zoume.core.tripsmgmt.presenter.detail.DetailTripContainerPresenter;
import com.yxhl.zoume.core.tripsmgmt.presenter.unit.CancelOrderPresenter;
import com.yxhl.zoume.core.tripsmgmt.ui.activity.TripDetailContainerActivity;
import com.yxhl.zoume.core.tripsmgmt.ui.adapter.PassengerDetailAdapter;
import com.yxhl.zoume.core.tripsmgmt.ui.event.CancelTripOrderEvent;
import com.yxhl.zoume.core.tripsmgmt.ui.event.CancelTripOrderFromDialogEvent;
import com.yxhl.zoume.core.tripsmgmt.ui.event.ComeBackFromTicketCodeEvent;
import com.yxhl.zoume.core.tripsmgmt.view.detail.DetailOrderView;
import com.yxhl.zoume.core.tripsmgmt.view.unit.CancelOrderView;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.di.component.trip.DaggerTripComponent;
import com.yxhl.zoume.di.module.ActivityModule;
import com.yxhl.zoume.di.module.trip.TripModule;
import com.yxhl.zoume.domain.interactor.ZMEventSubscriber;
import com.yxhl.zoume.utils.DeviceUtils;
import com.yxhl.zoume.utils.LOG;
import com.yxhl.zoume.utils.PromptUtils;
import com.yxhl.zoume.utils.StringUtils;
import com.yxhl.zoume.utils.ZMDialog;
import com.yxhl.zoume.utils.permission.PermissionHelper;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import rx.functions.Action1;

/**
 * created by sugarya
 * 定制巴士详情页
 */
public class ZouMeBusTripDetailFragment extends BaseFragment implements DetailOrderView, CancelOrderView {

    private static final String TAG = "CustomBusDetailTripFrag";


    //widgets
    //头部信息栏
    @BindView(R.id.rl_include_trip_detail_station_info)
    RelativeLayout stationInfoRl;

    @BindView(R.id.iv_include_trip_detail_station_info_icon)
    ImageView stationInfoIconIv;
    @BindView(R.id.tv_include_trip_detail_station_info_start)
    TextView stationInfoStartTv;
    @BindView(R.id.tv_include_trip_detail_station_info_clock)
    TextView stationInfoClockTv;
    @BindView(R.id.tv_include_trip_detail_station_info_date)
    TextView stationInfoDateTv;
    @BindView(R.id.tv_include_trip_detail_station_info_destination)
    TextView stationInfoDestinationTv;

    //行程信息栏
    @BindView(R.id.tv_custom_bus_trip_first_row_indicator)
    TextView tripFirstRowIndicatorTv;
    @BindView(R.id.tv_custom_bus_trip_first_row_content)
    TextView tripFirstRowContentTv;

    @BindView(R.id.tv_custom_bus_trip_second_row_indicator)
    TextView tripSecondRowIndicatorTv;
    @BindView(R.id.tv_custom_bus_trip_second_row_content)
    TextView tripSecondRowContentTv;

    @BindView(R.id.iv_custom_bus_trip_info_map)
    ImageView tripInfoMapIv;

    //乘客信息栏
    @BindView(R.id.tv_include_trip_detail_passenger_info_count_content)
    TextView passengerInfoCountContentTv;
    @BindView(R.id.iv_include_trip_detail_passenger_info_rotation)
    ImageView passengerInfoArrowIv;

    @BindView(R.id.lvw_custom_bus_passenger_info_detail)
    ListViewWrap passengerDetailLvw;

    //订单信息栏
    @BindView(R.id.tv_include_trip_detail_order_id_content)
    TextView orderIdContentTv;
    @BindView(R.id.tv_include_trip_detail_order_status_content)
    TextView orderStatusContentTv;
    @BindView(R.id.tv_include_trip_detail_order_create_time_content)
    TextView orderCreateTimeContentTv;
    @BindView(R.id.tv_include_trip_detail_order_payment_fee_content)
    TextView orderPaymentFeeContentTv;
    @BindView(R.id.tv_include_trip_detail_order_payment_shuttle)
    TextView orderPaymentShuttleTv;

    //评价司机栏
    @BindView(R.id.rl_include_trip_detail_judge_info)
    RelativeLayout judgeInfoRl;

    @BindView(R.id.cbl_include_trip_detail_judge)
    CheckBoxLayout judgeCbl;
    @BindView(R.id.tv_include_trip_detail_judge)
    TextView judgeTv;

    //底部信息栏
    @BindView(R.id.frame_custom_bus_bottom_container)
    FrameLayout bottomContainerFrame;

    @BindView(R.id.ll_custom_bus_bottom_created_bar)
    LinearLayout bottomCreatedBarLl;
    @BindView(R.id.tv_custom_bus_bottom_cancel_order)
    TextView bottomCancelOrderTv;
    @BindView(R.id.ll_custom_bus_bottom_paid_bar)
    LinearLayout bottomPaidBarLl;
    @BindView(R.id.ll_custom_bus_bottom_shuttle_bar_phone)
    LinearLayout bottomShuttleBarLl;

    //优惠券
    @BindView(R.id.ll_trip_detail_coupon)
    LinearLayout mLlTripDetailCoupon;  //详情优惠
    @BindView(R.id.tv_trip_detail_coupon_original)
    TextView mTvTripDetailCouponOriginal;  //原价
    @BindView(R.id.tv_trip_detail_coupon_discount)
    TextView mTvTripDetailCouponDiscount;  //差价
    @BindView(R.id.iv_trip_detail_payment_arrow)
    ImageView mIvTripDetailPaymentArrow;  // 展开箭头
    //arguments
    @Inject
    CancelOrderPresenter mCancelOrderPresenter;
    @Inject
    DetailTripContainerPresenter mDetailTripContainerPresenter;

    /**
     * 跳转 携带来的数据
     */
    private BizOrder mBizOrder;
    /**
     * 乘客集
     */
    private List<Contract> mPassengerList;

    /**
     * 取消订单等待对话框
     */
    private MaterialDialog mDialogForWaitingCancel;

    private PassengerDetailAdapter mPassengerDetailAdapter;

    /**
     * 乘客列表信息是否展开
     */
    private boolean needDisplayPassengerList = true;
    private int mFromDegree = 0;
    private int mToDegree = 180;

    private MaterialDialog dialogForWaitingCancel;

    /**
     * 行程订单详情信息
     */
    private BizOrderResponse mBizOrderResponse;

    public ZouMeBusTripDetailFragment() {
        // Required empty public constructor
    }

    public static ZouMeBusTripDetailFragment newInstance(BizOrder bizOrder) {
        Bundle args = new Bundle();
        args.putSerializable(UIConstants.TripContainer.ARGUMENT_KEY_CUSTOM_BUS_TRIP_ORDER, bizOrder);
        ZouMeBusTripDetailFragment fragment = new ZouMeBusTripDetailFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private void parseArguments() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mBizOrder = (BizOrder) arguments.getSerializable(UIConstants.TripContainer.ARGUMENT_KEY_CUSTOM_BUS_TRIP_ORDER);
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        parseArguments();
        onRxBusEvent();
    }

    private void onRxBusEvent() {
        //取消订单
        getSubscription().add(toSubscription(CancelTripOrderFromDialogEvent.class,
                new Action1<CancelTripOrderFromDialogEvent>() {
                    @Override
                    public void call(CancelTripOrderFromDialogEvent cancelTripOrderFromDialogEvent) {
                        if (mCancelOrderPresenter != null) {
                            mCancelOrderPresenter.cancelOrder(mBizOrder.getOrderSerial());
                        }
                    }
                },
                new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LOG.e(TAG, "throwable=" + throwable.toString());
                        onRxBusEvent();
                    }
                }
        ));
        //评价司机
        getSubscription().add(toSubscription(
                CommentOnDriverEvent.class,
                new Action1<CommentOnDriverEvent>() {
                    @Override
                    public void call(CommentOnDriverEvent commentOnDriverEvent) {
                        int point = commentOnDriverEvent.getPoint();
                        CommentEntrance commentEntrance = commentOnDriverEvent.getCommentEntrance();

                        switch (commentEntrance) {
                            case ZOUME_BUS_TRIP_DETAIL:
                                judgeInfoRl.setVisibility(View.VISIBLE);
                                showJudePoint(point);
                                break;
                        }
                    }
                },
                new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LOG.e(TAG, "throwable=" + throwable.toString());
                        onRxBusEvent();
                    }
                }
        ));
        //我的车票返回
        getSubscription().add(toSubscription(ComeBackFromTicketCodeEvent.class,
                new Action1<ComeBackFromTicketCodeEvent>() {
                    @Override
                    public void call(ComeBackFromTicketCodeEvent comeBackFromTicketCodeEvent) {
                        if (mBizOrder != null) {
                            String orderSerial = mBizOrder.getOrderSerial();
                            if (!TextUtils.isEmpty(orderSerial)) {
                                mDetailTripContainerPresenter.getDetailTripOrder(orderSerial);
                            }
                        }
                    }
                },
                new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LOG.e(TAG, "throwable=" + throwable.toString());
                        onRxBusEvent();
                    }
                }
        ));

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        ViewCompat.setTransitionName(stationInfoRl, UIConstants.TripContainer.TRIP_DETAIL_SHARE_ELEMENT);
        mDetailTripContainerPresenter.attachView(this);
        mCancelOrderPresenter.attachView(this);
        initializeShow(mBizOrder);
        bindData(mBizOrder);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mDetailTripContainerPresenter.getDetailTripOrder(mBizOrder.getOrderSerial());
    }


    private void startArrowAnimator() {
        ValueAnimator passengerArrowAnimator = ObjectAnimator.ofFloat(passengerInfoArrowIv, "rotation", mFromDegree, mToDegree);
        passengerArrowAnimator.setDuration(UIConstants.TRIP_DETAIL_ARROW_ROTATION_DURATION);
        passengerArrowAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                if (needDisplayPassengerList) {
                    passengerDetailLvw.setVisibility(View.VISIBLE);
                    mFromDegree = 180;
                    mToDegree = 360;
                } else {
                    passengerDetailLvw.setVisibility(View.GONE);
                    mFromDegree = 0;
                    mToDegree = 180;
                }
                needDisplayPassengerList = !needDisplayPassengerList;
            }
        });
        passengerArrowAnimator.start();
    }


    /**
     * 地图图标
     *
     * @param view
     */
    @OnClick(R.id.iv_custom_bus_trip_info_map)
    public void onTripInfoMapClick(View view) {
        boolean locationPermission = PermissionHelper.checkLocationPermission(mActivity);
        if (locationPermission) {
            getAppComponent().navigator().navigateToZouMeBusMapContainerActivity(
                    mActivity,
                    mBizOrderResponse,
                    ZouMeBusMapEntrance.ZOUME_BUS_TRIP_MANAGEMENT
            );
        } else {
            PermissionHelper.takeLocationPermission(this, PermissionHelper.REQUEST_CODE_ZOU_ME_POSITION_LOCATION);
        }

    }

    /**
     * 乘车人箭头
     *
     * @param view
     */
    @OnClick(R.id.ll_include_trip_detail_passenger_info)
    public void onPassengerArrow(View view) {
        startArrowAnimator();
    }

    /**
     * 去评价
     *
     * @param view
     */
    @OnClick(R.id.tv_include_trip_detail_judge)
    public void onJudgeClick(View view) {
        getAppComponent().navigator().navigateToCommentOnDriverActivity(mActivity, mBizOrder, CommentEntrance.ZOUME_BUS_TRIP_DETAIL);
    }

    /**
     * 取消订单
     *
     * @param view
     */
    @OnClick(R.id.tv_custom_bus_bottom_cancel_order)
    public void onCancelOrderClick(View view) {
        ZMDialog dialog = new ZMDialog.Builder(mActivity)
                .setMessage(getString(R.string.dialog_cancel_trip_order_content))
                .setPositiveMessage(getString(R.string.dialog_confirm))
                .setNegativeMessage(getString(R.string.dialog_cancel))
                .setPositiveClickEvent(new ZMEventSubscriber(){
                    @Override
                    public void onNext(Object o) {
                    //    super.onNext(o);
                        RxBus.getInstance().send(new CancelTripOrderFromDialogEvent());
                    }
                })
                .setNegativeClickEvent(new ZMEventSubscriber())
                .show();

    }

    /**
     * 去支付
     *
     * @param view
     */
    @OnClick(R.id.tv_custom_bus_bottom_to_pay)
    public void onToPayClick(View view) {
        getAppComponent().navigator().navigateToPayment(getActivity(), mBizOrder);
    }

    /**
     * 拨接驳电话
     *
     * @param view
     */
    @OnClick(R.id.ll_custom_bus_bottom_shuttle_bar_phone)
    public void onShuttleBarPhone(View view) {
        BizOrder ferryOrder = mBizOrder.getFerryOrder();
        if (ferryOrder != null) {
            String driverMobile = ferryOrder.getDriverMobile();
            if (!TextUtils.isEmpty(driverMobile)) {
                boolean phonePermission = PermissionHelper.checkPhonePermission(mActivity);
                if (!phonePermission) {
                    requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, PermissionHelper.REQUEST_CODE_CALL_TRIP_DETAIL);
                } else {
                    DeviceUtils.dialTelPhone(mActivity, driverMobile);
                }
            }
        }
    }

    /**
     * 我的车票
     *
     * @param view
     */
    @OnClick(R.id.tv_custom_bus_bottom_my_ticket)
    public void onMyTicketClick(View view) {
        getAppComponent().navigator().navigateToTicketCodeActivity(
                mActivity, mBizOrder.getOrderSerial(), mBizOrder.getBizType());
    }


    private void bindData(BizOrder bizOrder) {
        OrderPayStatus payStatus = bizOrder.getPayStatus();
        //头部站点信息
        setStartStation(bizOrder.getStart());
        setDestination(bizOrder.getDestination());
        setStartTimeAndData(bizOrder.getGmtDepart());

        //巴士信息
        setTripFirstRowContent(bizOrder.getVehicleNo());
        setTripSecondRowContent(bizOrder);

        //乘车人数
        setPassengerCount(bizOrder.getAmount());

        //订单状态信息
        setOrderId(bizOrder.getOrderSerial());
        setOrderStatus(payStatus);
        setGmtCreate(bizOrder.getGmtCreate());
        setTotalFee(bizOrder.getTotalFee());

        setShuttlePrice(bizOrder.getFerryOrder());

        //评论
        JudgeEntry judgeEntry = bizOrder.getJudge();
        setDriverJudge(judgeEntry);
        //coupon
        setCouponInfo(bizOrder);
    }

    private void setCouponInfo(BizOrder bizOrder) {
        mTvTripDetailCouponOriginal.setText("¥"+bizOrder.getOrderTotalFee());
        mTvTripDetailCouponDiscount.setText("－¥"+bizOrder.getDiscountFee());

    }

    private void setShuttlePrice(BizOrder ferryBizOrder) {
        if (ferryBizOrder != null) {
            double price = ferryBizOrder.getTotalFee();
            if (price != 0) {
                orderPaymentShuttleTv.setText("含接驳车费用" + price + "元");
            } else {
                orderPaymentShuttleTv.setVisibility(View.INVISIBLE);
            }
        } else {
            orderPaymentShuttleTv.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 显示出发地
     *
     * @param station
     */
    private void setStartStation(String station) {
        if (!TextUtils.isEmpty(station)) {
            stationInfoStartTv.setText(station);
        } else {
            stationInfoStartTv.setText("");
        }
    }

    /**
     * 显示目的地
     *
     * @param station
     */
    private void setDestination(String station) {
        if (!TextUtils.isEmpty(station)) {
            stationInfoDestinationTv.setText(station);
        } else {
            stationInfoDestinationTv.setText("");
        }
    }

    /**
     * 显示出发时刻和日期
     *
     * @param timeStamp
     */
    private void setStartTimeAndData(String timeStamp) {
        if (!TextUtils.isEmpty(timeStamp)) {
            String[] strings = timeStamp.split(" ");
            if (strings != null) {
                stationInfoDateTv.setText(strings[0]);
                stationInfoClockTv.setText(strings[1]);
            }
        } else {
            stationInfoDateTv.setText("-");
            stationInfoClockTv.setText("-");
        }
    }

    private void setTripFirstRowContent(String vehicle) {
        if (!TextUtils.isEmpty(vehicle)) {
            tripFirstRowContentTv.setText(vehicle);
        } else {
            tripFirstRowContentTv.setText("正在调派中");
        }
    }

    private void setTripSecondRowContent(BizOrder bizOrder) {
        OrderBizType bizType = bizOrder.getBizType();
        switch (bizType) {
            case OBT_DIY_BUS:
                boolean ndFerry = bizOrder.getNdFerry();
                if (ndFerry) {
                    String showContent;
                    BizOrder ferryOrder = bizOrder.getFerryOrder();
                    String driverCalledName = StringUtils.getCalledName(ferryOrder.getDriverName());
                    String vehicleNo = ferryOrder.getVehicleNo();
                    showContent = driverCalledName + "    " + vehicleNo;
                    if (TextUtils.isEmpty(vehicleNo) && TextUtils.isEmpty(driverCalledName)) {
                        showContent = "正在调派中";
                    }
                    tripSecondRowContentTv.setText(showContent);
                } else {
                    String getOnStation = bizOrder.getGetOnStation();
                    if (!TextUtils.isEmpty(getOnStation)) {
                        tripSecondRowContentTv.setText(getOnStation);
                    }
                }
                break;
            case OBT_FT_BUS:
                String driverCalledName = StringUtils.getCalledName(bizOrder.getDriverName());
                tripSecondRowContentTv.setText(driverCalledName);
                break;
        }

    }

    /**
     * 显示乘客人数
     *
     * @param amount
     */
    private void setPassengerCount(int amount) {
        passengerInfoCountContentTv.setText(amount + "人");
    }

    /**
     * 显示订单编号
     *
     * @param orderId
     */
    private void setOrderId(String orderId) {
        if (!TextUtils.isEmpty(orderId)) {
            orderIdContentTv.setText(orderId);
        }
    }

    /**
     * 显示行程状态
     *
     * @param payStatus
     */
    private void setOrderStatus(OrderPayStatus payStatus) {
        String orderStatusStr;
        switch (payStatus) {
            case CREATED:
                orderStatusStr = getString(R.string.trip_detail_pay_status_created);
                break;
            case PAID:
                orderStatusStr = getString(R.string.trip_detail_pay_status_paid);
                break;
            case PAY_INVALID:
                orderStatusStr = getString(R.string.trip_detail_pay_status_invalid);
                break;
            case OPS_REFUNDING:
                orderStatusStr = getString(R.string.trip_detail_pay_status_refunding);
                break;
            case OPS_REFUND:
                orderStatusStr = getString(R.string.trip_detail_pay_status_refund);
                break;
            case PART_FINISH:
                orderStatusStr = getString(R.string.trip_detail_pay_status_part_finish);
                break;
            case ORDER_FINISH:
                orderStatusStr = getString(R.string.trip_detail_pay_status_finish);
                break;
            case PAY_CANCEL:
                orderStatusStr = getString(R.string.trip_detail_pay_status_cancel);
                break;
            case OPS_PART_REFUND:
                orderStatusStr = getString(R.string.trip_detail_pay_status_part_refunding);
                break;
//            case OPS_NO_REFUND:
//                orderStatusStr = "";
//                break;
            default:
                orderStatusStr = "";
        }
        orderStatusContentTv.setText(orderStatusStr);
    }

    /**
     * 显示下单时间
     *
     * @param time
     */
    private void setGmtCreate(String time) {
        if (!TextUtils.isEmpty(time)) {
            orderCreateTimeContentTv.setText(time);
        }
    }

    /**
     * 显示总费用
     *
     * @param fee
     */
    private void setTotalFee(double fee) {
        if (fee >= 0) {
            orderPaymentFeeContentTv.setText(fee + "元");
        } else {
            orderPaymentFeeContentTv.setText("");
        }
    }

    /**
     * 评价司机
     *
     * @param judgeEntry
     */
    private void setDriverJudge(JudgeEntry judgeEntry) {
        if (judgeEntry != null) {
            int judgePoint = judgeEntry.getJudgePoint();
            if (judgePoint > 0) {
                showJudePoint(judgePoint);
            } else {
                judgeCbl.setVisibility(View.INVISIBLE);
                judgeTv.setVisibility(View.VISIBLE);
            }
        } else {
            judgeCbl.setVisibility(View.INVISIBLE);
            judgeTv.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 显示评价星星数
     *
     * @param judgePoint
     */
    private void showJudePoint(int judgePoint) {
        judgeCbl.setCheckedCount(judgePoint);
        judgeCbl.setVisibility(View.VISIBLE);
        judgeTv.setVisibility(View.INVISIBLE);
    }

    /**
     * 初始化UI展示
     *
     * @param bizOrder
     */
    private void initializeShow(BizOrder bizOrder) {
        OrderPayStatus payStatus = bizOrder.getPayStatus();
        OrderBizType bizType = bizOrder.getBizType();

        //改变顶部信息
        exchangeStationInfoIcon(bizType);
        //改变巴士信息/接驳信息栏
        boolean ndFerry = bizOrder.getNdFerry();
        exchangeTripInfoStyle(ndFerry, bizType, payStatus);
        //改变乘车人箭头状态
        exchangePassengerArrowStyle(bizOrder);
        //支付金额 接驳车
        exchangeOrderFeeStyle(bizOrder);
        //司机评价栏
        exchangeJudgeDriverStyle(payStatus);
        //改变底部信息栏信息
        exchangeBottomBarStyle(bizOrder);
    }

    private void exchangeJudgeDriverStyle(OrderPayStatus payStatus) {
        switch (payStatus) {
            case ORDER_FINISH:
                judgeInfoRl.setVisibility(View.VISIBLE);
                break;
            default:
                judgeInfoRl.setVisibility(View.GONE);
                break;
        }
    }

    private void exchangeBottomBarStyle(BizOrder bizOrder) {
        OrderBizType bizType = bizOrder.getBizType();
        OrderPayStatus payStatus = bizOrder.getPayStatus();

        boolean ndFerry = bizOrder.getNdFerry();

        switch (payStatus) {
            case CREATED:
                switch (bizType) {
                    case OBT_DIY_BUS:
                        bottomContainerFrame.setVisibility(View.VISIBLE);
                        bottomCreatedBarLl.setVisibility(View.VISIBLE);
                        break;
                    case OBT_FT_BUS:
                        bottomContainerFrame.setVisibility(View.VISIBLE);
                        bottomCreatedBarLl.setVisibility(View.VISIBLE);
                        break;
                }
                break;
            case PAID:
            case PART_FINISH:
            case OPS_PART_REFUND:
                switch (bizType) {
                    case OBT_DIY_BUS:
                        bottomContainerFrame.setVisibility(View.VISIBLE);
                        bottomPaidBarLl.setVisibility(View.VISIBLE);
                        //是否显示接驳司机
                        if (ndFerry) {
                            BizOrder ferryOrder = bizOrder.getFerryOrder();
                            if (ferryOrder != null) {
                                String driverMobile = ferryOrder.getDriverMobile();
                                if (!TextUtils.isEmpty(driverMobile)) {
                                    bottomShuttleBarLl.setVisibility(View.VISIBLE);
                                } else {
                                    bottomShuttleBarLl.setVisibility(View.GONE);
                                }
                            } else {
                                bottomShuttleBarLl.setVisibility(View.GONE);
                            }
                        } else {
                            bottomShuttleBarLl.setVisibility(View.GONE);
                        }
                        break;
                    case OBT_FT_BUS:
                        bottomContainerFrame.setVisibility(View.VISIBLE);
                        bottomPaidBarLl.setVisibility(View.VISIBLE);
                        bottomShuttleBarLl.setVisibility(View.GONE);
                        break;
                }
                break;
            default:
                bottomContainerFrame.setVisibility(View.GONE);
        }
    }


    private void exchangeOrderFeeStyle(BizOrder bizOrder) {
        OrderBizType bizType = bizOrder.getBizType();
        switch (bizType) {
            case OBT_DIY_BUS:
                orderPaymentShuttleTv.setVisibility(View.VISIBLE);
                break;
            case OBT_FT_BUS:
                orderPaymentShuttleTv.setVisibility(View.INVISIBLE);
                break;
        }
    }

    private void exchangePassengerArrowStyle(BizOrder bizOrder) {
        mPassengerList = bizOrder.getPassengerList();
        if (mPassengerList != null && mPassengerList.size() > 0) {
            if (mPassengerDetailAdapter == null) {
                mPassengerDetailAdapter = new PassengerDetailAdapter(
                        mActivity,
                        mPassengerList,
                        getChildFragmentManager(),
                        mBizOrder.getOrderSerial()
                );
                passengerDetailLvw.setAdapter(mPassengerDetailAdapter);
            } else {
                mPassengerDetailAdapter.notifyDataSetChanged();
            }
            passengerInfoArrowIv.setVisibility(View.VISIBLE);
        } else {
            passengerInfoArrowIv.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 改变行程详情类型图标
     *
     * @param bizType
     */
    private void exchangeStationInfoIcon(OrderBizType bizType) {
        switch (bizType) {
            case OBT_DIY_BUS:
                stationInfoIconIv.setImageResource(R.drawable.ic_dingzhibashibiaoqian);
                break;
            case OBT_FT_BUS:
                stationInfoIconIv.setImageResource(R.drawable.ic_bashibiaoqian);
                break;
        }
    }


    private void exchangeTripInfoStyle(boolean ndFerry, OrderBizType bizType, OrderPayStatus payStatus) {

        exchangeTripFirstRowStyle(ndFerry, bizType);
        //展示司机信息或者接驳车信息
        exchangeTripSecondRow(ndFerry, bizType);
        //改变位置信息地图图标
        exchangeTripMapStyle(payStatus);
    }

    private void exchangeTripFirstRowStyle(boolean ndFerry, OrderBizType bizType) {
        switch (bizType) {
            case OBT_DIY_BUS:
                if (ndFerry) {
                    tripFirstRowIndicatorTv.setText(R.string.trip_detail_first_row_shuttle_indicator);
                } else {
                    tripFirstRowIndicatorTv.setText(R.string.trip_detail_first_row_indicator);
                }
                break;
            case OBT_FT_BUS:
                tripFirstRowIndicatorTv.setText(R.string.trip_detail_first_row_indicator);
                break;
        }
    }

    /**
     * 展示司机信息栏或者接驳车信息栏
     *
     * @param bizType
     */
    private void exchangeTripSecondRow(boolean ndFerry, OrderBizType bizType) {
        switch (bizType) {
            case OBT_DIY_BUS:
                if (ndFerry) {
                    tripSecondRowIndicatorTv.setText(R.string.trip_detail_shuttle_indicator);
                } else {
                    tripSecondRowIndicatorTv.setText(R.string.trip_detail_custom_bus_indicator);
                }
                break;
            case OBT_FT_BUS:
                tripSecondRowIndicatorTv.setText(R.string.trip_detail_driver_name_indicator);
                break;
        }
    }

    private void exchangeTripMapStyle(OrderPayStatus payStatus) {
        switch (payStatus) {
            case CREATED:
            case PAID:
            case PART_FINISH:
            case OPS_PART_REFUND:
                tripInfoMapIv.setClickable(true);
                tripInfoMapIv.setImageResource(R.drawable.ic_kechakanditu);
                break;
            default:
                tripInfoMapIv.setClickable(false);
                tripInfoMapIv.setImageResource(R.drawable.ic_bukechakanditu);
                break;
        }
    }

    @Override
    public void renderDetailOrderOnSuccess(BizOrderResponse bizOrderResponse) {
        LOG.e(TAG, "renderDetailOrderOnSuccess bizOrderResponse=" + bizOrderResponse.toString());
        mBizOrderResponse = bizOrderResponse;
        List<BizOrder> bizOrderList = bizOrderResponse.getOrderFieldsesList();
        if (bizOrderList != null && bizOrderList.size() > 0) {
            mBizOrder = bizOrderList.get(0);
            initializeShow(mBizOrder);
            bindData(mBizOrder);
        }
    }

    @Override
    public void renderDetailOrderOnFailure() {
    }


    @Override
    public void renderCancelOrderResult(boolean isCanceled) {
        if (isCanceled) {
            PromptUtils.showShort(mActivity, getString(R.string.cancel_order_success));
            RxBus.getInstance().send(new CancelTripOrderEvent(mBizOrder.getOrderSerial()));
            mActivity.finish();
        } else {
            PromptUtils.showLong(getActivity(), getString(R.string.trip_cancel_order_failure));
        }
    }

    @Override
    public void renderCancelError() {
        PromptUtils.showShort(mActivity, getString(R.string.trip_cancel_order_error));
    }

    @Override
    public void waitingForOrderCancel(boolean isWaiting) {
        if (isWaiting) {
            mDialogForWaitingCancel = new MaterialDialog.Builder(getActivity())
                    .content(R.string.dialog_waiting_canceling)
                    .progress(true, 0)
                    .canceledOnTouchOutside(false)
                    .show();
        } else {
            if (null != mDialogForWaitingCancel) {
                mDialogForWaitingCancel.dismiss();
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PermissionHelper.REQUEST_CODE_CALL_TRIP_DETAIL:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    String phoneNum = mBizOrder.getDriverName();
                    DeviceUtils.dialTelPhone(mActivity, phoneNum);
                }
                break;
            case PermissionHelper.REQUEST_CODE_ZOU_ME_POSITION_LOCATION:
                getAppComponent().navigator().navigateToZouMeBusMapContainerActivity(
                        mActivity,
                        mBizOrderResponse,
                        ZouMeBusMapEntrance.ZOUME_BUS_TRIP_MANAGEMENT
                );
                break;
        }
    }


    @Override
    protected void initializeInjector() {
        DaggerTripComponent
                .builder()
                .appComponent(getAppComponent())
                .activityModule(new ActivityModule(mActivity))
                .tripModule(new TripModule())
                .build()
                .inject(this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_custom_bus_detail_trip;
    }

    @Override
    protected void setupToolBar() {
        ((TripDetailContainerActivity) mActivity).setToolbar(getString(R.string.trip_detail_header_title), R.drawable.icon_share);
    }


    @OnClick(R.id.ll_include_trip_detail_order_payment_fee)
    public void onDetailCuoponClick() {
        int visibility = mLlTripDetailCoupon.getVisibility();
        if (visibility == View.GONE) {
            mLlTripDetailCoupon.setVisibility(View.VISIBLE);
            triangleAnimationStart();
        } else {
            mLlTripDetailCoupon.setVisibility(View.GONE);
            triangleAnimationEnd();
        }

    }

    private void triangleAnimationStart() {
        RotateAnimation rotateAnimation=	new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(200);
        rotateAnimation.setFillAfter(true);
        mIvTripDetailPaymentArrow.startAnimation(rotateAnimation);
    }
    private void triangleAnimationEnd() {
        RotateAnimation rotateAnimation=	new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(200);
        rotateAnimation.setFillAfter(true);
        mIvTripDetailPaymentArrow.startAnimation(rotateAnimation);
    }

}
